package com.hugh.lagouserviceuser.controller;

import com.hugh.lagouserviceuser.pojo.LagouToken;
import com.hugh.lagouserviceuser.service.CodeServiceFeignClient;
import com.hugh.lagouserviceuser.service.LagouTokenService;
import com.hugh.lagouserviceuser.util.CheckCodeResult;
import com.hugh.lagouserviceuser.util.Md5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @Author: Hugh
 * @Date: 2020/7/19
 */
@RestController
@RequestMapping("/api/user")
@Slf4j
public class RegisterController {

    @Autowired
    private CodeServiceFeignClient codeServiceFeignClient;

    @Autowired
    private LagouTokenService lagouTokenService;

    /**
     * 注册接⼝
     * @param email 邮箱
     * @param password 密码
     * @param code 验证码
     * @return true成功，false失败
     */
    @GetMapping("/register/{email}/{password}/{code}")
    public String register(@PathVariable String email,
                            @PathVariable String password,
                            @PathVariable String code,
                           HttpServletResponse response){
        // 已经注册过
        if(isRegistered(email)) {
            return "-1";
        }
        //1、先验证token是否有效
        Integer status = codeServiceFeignClient.validate(email, code);
        if(!CheckCodeResult.SUCCESS.getValue().equals(status)){
            return status.toString();
        }
        //2、然后进行保存
        String token = lagouTokenService.saveToken(email, password);
        log.info("邮箱:{} 注册成功", email);
        //3、处理token
        Cookie cookie = new Cookie("login_token",token);
        cookie.setPath("/");
        response.addCookie(cookie);
        Cookie emailCookie = new Cookie("login_email",email);
        emailCookie.setPath("/");
        response.addCookie(emailCookie);
        return "0";
    }

    /**
     * 是否已注册，根据邮箱判断,
     * @param email 邮箱
     * @return true代表已经注册过，
     *       false代表尚未注册
     */
    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable String email){
        LagouToken userByEmail = lagouTokenService.findUserByEmail(email);
        return Objects.nonNull(userByEmail);
    }

    /**
     * 登录接⼝，验证⽤户名密码合法性，根据⽤户名和
     * 密码⽣成token，token存⼊数据库，并写⼊cookie
     * 中，登录成功返回邮箱地址，重定向到欢迎⻚
     * @param email 邮箱
     * @param password 密码
     * @return 邮箱地址
     */
    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable String email, @PathVariable String password, HttpServletRequest request, HttpServletResponse response){
        LagouToken userByEmail = lagouTokenService.findUserByEmail(email);
        if(Objects.isNull(userByEmail)){
            return "false";
        }
        Boolean agree = Md5.checkIsPasswork(userByEmail.getToken(), Md5.getBeforeEncode(email, password));
        if(agree){
            Cookie cookie = new Cookie("login_token", userByEmail.getToken());
            cookie.setPath("/");
            response.addCookie(cookie);
            Cookie emailCookie = new Cookie("login_email", userByEmail.getEmail());
            emailCookie.setPath("/");
            response.addCookie(emailCookie);
            //跳转到欢迎界面s
            return "true";
        }
        return "false";
    }

    /**
     * 根据token查询⽤户登录邮箱接⼝
     * @param token token
     * @return 邮箱地址
     */
    @GetMapping("/info/{token}")
    public String info(@PathVariable String token){
        LagouToken userByToken = lagouTokenService.findUserByToken(token);
        if(Objects.nonNull(userByToken)){
            return userByToken.getEmail();
        }
        return "";
    }
}
